package com.qijian.common.utils.easyexcel.handler;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.style.column.AbstractHeadColumnWidthStyleStrategy;

import java.util.Collection;

/**
 * @author gw
 */
public class HideColumnHandler extends AbstractHeadColumnWidthStyleStrategy {

    private final Collection<Integer> hideColumnIndex;

    private final boolean isNeedHide;

    private final Integer defaultWidth;

    public HideColumnHandler(Integer defaultWidth) {
        this.defaultWidth = defaultWidth;
        this.hideColumnIndex = null;
        this.isNeedHide = false;
    }

    public HideColumnHandler(Integer defaultWidth, Collection<Integer> hideColumnIndex) {
        this.defaultWidth = defaultWidth;
        this.hideColumnIndex = hideColumnIndex;
        this.isNeedHide = hideColumnIndex != null && hideColumnIndex.size() > 0;
    }

    @Override
    protected Integer columnWidth(Head head, Integer columnIndex) {
        if (isNeedHide) {
            if (hideColumnIndex.contains(columnIndex)) {
                return 0;
            }
        }
        return defaultWidth;
    }
}
